<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<style>
    <!--
    object {
        left: 0;
        margin: 0;
        padding: 0;
        border: 0;
        font-size: 14px;
        font: inherit;
        vertical-align: baseline;
    }

    legend {
        margin: 10px;
    }
</style>
<fieldset>
    <legend>会员信息列表</legend>
    <!-- 表单搜索 开始 -->
    <section class="panel panel-padding ">
        <form class="layui-form layui-form-pane form-search" onsubmit="return false" method="get" id="tvUserForm">
            <div class="layui-form-item layui-inline">
                <label class="layui-form-label">会员卡号</label>
                <div class="layui-input-inline">
                    <input name="userCardNumber" id="userCardNumber" value="" placeholder="请输入会员卡号"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item layui-inline" style="background-color: #1E9FFF">
                <button class="layui-btn layui-btn-primary" lay-submit lay-filter="search01" id="serch">
                    <i class="layui-icon"></i> 查询
                </button>
            </div>
        </form>
    </section>
    <section class="panel panel-padding " id="tvUserData">
        <!-- 表单搜索 结束 -->
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">会员tvn号</label>
            <div class="layui-input-inline">
                <input name="userId" id="userId" value="" readonly
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">会员卡号</label>
            <div class="layui-input-inline">
                <input name="userCardNumber" id="userCardNumber01" value="" readonly
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">会员姓名</label>
            <div class="layui-input-inline">
                <input name="userName" id="userName" value="" readonly
                       class="layui-input">
            </div>
        </div>
        <br>
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">会员电话</label>
            <div class="layui-input-inline">
                <input name="userPhone" id="userPhone" value="" readonly
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">账号余额</label>
            <div class="layui-input-inline">
                <input name="userBalance" id="userBalance" value="" readonly
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">会员积分</label>
            <div class="layui-input-inline">
                <input name="userIntegral" id="userIntegral" value="" readonly
                       class="layui-input">
            </div>
        </div>
        <br>
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">会员地址</label>
            <div class="layui-input-inline">
                <input name="addressDetail" id="addressDetail" value="" readonly
                       class="layui-input" style="width: 820px">
            </div>
        </div>
    </section>
</fieldset>

<br>
<br>

<fieldset>
    <legend>积分兑换商品列表</legend>
    <!-- 表单搜索 开始 -->
    <section class="panel panel-padding ">
       <form class="layui-form layui-form-pane form-search" onsubmit="return false" method="post">
            <div class="layui-form-item layui-inline">
                <label class="layui-form-label">商品名称</label>
                <div class="layui-input-inline">
                    <input name="goodsName" id="goodsName" value="" placeholder="请输入商品名称"
                           class="layui-input">
                </div>
            </div>
           <!--  <div class="layui-form-item layui-inline">
						<label class="layui-form-label">商品属性</label>
						<div class="layui-input-inline">
							<div style="width:150px">
								<select name="type" id="type">
									<option value="">请选择</option>
									<option value="正品">正品</option>
									<option value="打折">打折</option>
									<option value="体验">体验</option>
									<option value="积分兑换">积分兑换</option>
								</select>
							</div>
						</div>
					</div> -->
					
            <div class="layui-form-item layui-inline" style="background-color: #1E9FFF">
                <button class="layui-btn layui-btn-primary" lay-submit lay-filter="search02" id="search02">
                    <i class="layui-icon"></i> 查询
                </button>
            </div>
        </form>
    </section>
    <section class="panel panel-padding " style="width:1200px">
        <!-- 表单搜索 结束 -->
        <table class="layui-table" id="tableReload" lay-filter="popedomLay"></table>
    </section>
</fieldset>
<script type="text/html" id="popedomBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="takeOrder">下单</a>
</script>

<script type="text/javascript">
    layui.config({
        base: "js/common/"
    }).use(["common", "element", "layer", "form", "table", "jquery"], function () {
        var elem = layui.element;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var $ = layui.jquery;
        layui.common.init();

        var url = 'xzxd/tofindGoods.do?areaId=' + ${areaId};
        console.log(${areaId})
        table.render({
            elem: "#tableReload"
            , width: 'auto'
            , url: url
            , page: true
            , even: true
            , skin: 'row'
            , limits: [10, 20, 30, 40, 50]
            , limit: 10
            , cols: [[
                {type: 'numbers', title: '序号', width: '120', sort: true}
                , {field: 'goods_name', title: '商品名称', width: 150, align: 'center'}
                  , {field: 'goods_id', title: '商品id', width: 150, align: 'center'}
                , {field: 'specifications', title: '商品规格', width: 120, align: 'center'}
              //  , {field: 'goodsExchIntegral', title: '所需积分（积分）', width: 150, align: 'center'}
                , {field: 'common_price', title: '商品价格', width: 150, align: 'center'}
                , {field: 'type', title: '商品属性', width: 150, align: 'center'}
                , {field: 'shipping_fee', title: '运费（￥）', width: 120, align: 'center'}
                , {field: 'goods_destine_quota', title: '预定限额', width: 120, align: 'center'}
                , {field: 'goods_storage', title: '库存', width: 120, align: 'center', sort: true}
                , {fixed: 'right', width: 150, title: '操作', align: 'center', toolbar: '#popedomBar'}
            ]]
            , done: function (res, curr, count) {
                //console.log(res); //接口返回信息
                var data = res.data;
                /* console.log(data)*/
                /*隐藏列*/
                $("[data-field='goodsId']").css('display', 'none');
            }
        });

        /**
         * 监听刷新表格
         */
        form.on('submit(search02)', function (data) {
            //console.log(data.field)
            table.reload('tableReload', {
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });

        //为了下单后同步信息，设置定时器重新请求数据
        var a;

        function click() {
            a = setTimeout(function () {
                $('#serch').click()
                $('#search02').click()
                /*移除sessionStorage*/
                sessionStorage.removeItem("tvUserMsg");
                sessionStorage.removeItem("toBuyGoodsMsg");
            }, 1000);
        }

        /**
         * 监听刷新会员信息
         */
        form.on('submit(search01)', function (data) {
            //console.log(data.field)
            $.ajax({
                url: "XD020/toQueryUser.do",
                type: "post",
                data: data.field,
                dataType: 'json',
                success: function (json) {
                    var data = json.model[0];
                    
                    sessionStorage.setItem('tvUserMsg', JSON.stringify(data));//将数据存入sessionStorage
                    if (json.statusCode == 200) {
                        //layer.msg(json.message, {icon: 6, time: 2000});
                        $("#userId").attr("value", data.userId);
                        $("#userCardNumber01").attr("value", data.userCardNumber);
                        $("#userName").attr("value", data.userName);
                        $("#userPhone").attr("value", data.userPhone);
                        $("#userBalance").attr("value", data.userBalance);
                        $("#userIntegral").attr("value", data.userIntegral);
                        $("#addressDetail").attr("value", data.addressDetail);
                    } else {
                        layer.msg(json.message, {icon: 5, time: 2000});
                        //重置input标签数据
                        //方法1
                        $(':input', '#tvUserData').not(':button, :submit, :reset, :hidden,:radio').attr("value", ''); // 去除不需要重置的input类型
                        //方法2
                        /*$("#tvUserData").find('input,select').each(function() {
                           $(this).attr("value", '');
                           });*/
                        /*let serch = document.getElementById("serch");
                        serch.click()*/
                    }
                }
            });
            clearTimeout(a);
        });


        //点击 '下单' 按钮触发事件
        table.on('tool(popedomLay)', function (obj) {
            var data = obj.data;
            console.log(data)
            sessionStorage.setItem('toBuyGoodsMsg', JSON.stringify(data));//将数据存入sessionStorage
            var userCardNumber = $('#userCardNumber').val(); //tvn号码
            var userBalance = $('#userBalance').val(); //获取余额
            var goodsDestineQuota = data.goods_destine_quota;//预定限额
            var commonPrice = data.common_price; //所需商品价格
            var type = data.type

            if (userCardNumber == '') {
                layer.msg("请先查询会员信息！", {icon: 5, time: 2000});
                return false;
            }
            if(type=="积分兑换品"){
            layer.msg("请到积分兑换页面进行兑换！", {icon: 5,time: 2000});
            return false;
            }
            if (userBalance < commonPrice) {
                layer.msg("余额不足！", {icon: 5, time: 2000});
                table.reload("tableReload");
            } else {
                //console.log(data.goodsId)
                if (obj.event === 'takeOrder') {
                    //console.log("下单了")
                    layer.open({
                        type: 2,
                        title: '下单详情',
                        content: 'xzxd/toBuyPage.do',
                        area: ['750px', '582px'],
                        anim: 5,
                        isOutAnim: true,
                        maxmin: true,
                        resize: false,
                        moveOut: true,
                        zIndex: 99999,
                        btn: ['确定', '关闭'],
                        success: function (layero, index) {
                            var body = layer.getChildFrame('body', index);  //巧妙的地方在这里哦
                            // console.log(data.goodsId)
                            body.contents().find("#goods_id").val(data.goods_id);
                            body.contents().find("#goods_storage").val(data.goods_storage);
                            body.contents().find("#common_price").val(data.common_price);
                            body.contents().find("#shipping_fee").val(data.shipping_fee);
                            body.contents().find("#goods_type").val(data.goods_type);
                            layui.form.render();
                        },
                        yes: function (index, layero) {         //按了弹出层的确定按钮时，这是将在父窗口中获取子窗口form标签里的所有值，并根据name名和值形成键值对json对象
                            /*console.log(layero);*/
                            //layer.alert('来到这里了'+index);
                            let body = layer.getChildFrame("body", index);
                            let data = {};
                            body.find("#my_form").serializeArray().forEach(function (item) {    //获取弹出层写下的数据，input，下拉框啊，之类的表单元素（即changefileform下的所有数据）
                                data[item.name] = item.value;   //根据表单元素的name属性来获取数据
                            });
                            //console.log(data);
                            /*判空*/
                            if (data.number == '') {
                                layer.msg("购买数量不能为空！", {icon: 5, time: 2000});
                                return false;
                            }
                            if (data.getWaySelect == '') {
                                layer.msg("配送方式不能为空！", {icon: 5, time: 2000});
                                return false;
                            }
                            /*限额判断*/
                            if (parseInt(data.number) > parseInt(goodsDestineQuota)) {
                                layer.msg("超过预定限额：" + goodsDestineQuota, {icon: 5, time: 2000});
                                return false;
                            }
                            toBuy(data);
                            layer.close(index);
                            click();
                        },
                        end: function () {
                            table.reload("tableReload");
                            return false;
                        }
                    });
                }
            }
        });
    });

    function toBuy(data) {
        console.log(data)
        /*获取sessionStorage值*/
        var tvUserMsg = JSON.parse(sessionStorage.getItem('tvUserMsg'));
        var toBuyGoodsMsg = JSON.parse(sessionStorage.getItem('toBuyGoodsMsg'));

        $.ajax({
            url: "xzxd/toBuy.do",
            type: "post",
            data: {tvUserMsg, toBuyGoodsMsg, data},
            success: function (data) {
                if (data.statusCode == '200') {
                    // console.log("值返回成功")
                    layer.msg(data.message, {icon: 6, time: 2000});
                } else {
                    layer.msg(data.message, {icon: 5, time: 2000});
                }
            }
        });

    }
</script>




